package com.bls.cate.listener;

import android.app.Activity;
import android.support.design.widget.AppBarLayout;

import com.bls.cate.constant.Interface;
import com.bls.cate.ui.activity.person.PersonActivity;
import com.bls.cate.ui.activity.person_other.PersonOtherActivity;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/17.
 */

public class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

    public enum State {
        EXPANDED,//展开状态
        COLLAPSED,//折叠状态
        IDLE//中间状态
    }

    private Activity mActivity;
    private OnStateChangeListener onStateChangeListener;
    private State mCurrentState = State.IDLE;

    public AppBarStateChangeListener(Activity mActivity, OnStateChangeListener onStateChangeListener) {
        this.mActivity = mActivity;
        this.onStateChangeListener = onStateChangeListener;
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (mActivity instanceof PersonActivity || mActivity instanceof PersonOtherActivity) {
            if (verticalOffset == 0) {
                if (mCurrentState != State.EXPANDED) {
                    onStateChangeListener.onStateChanged(appBarLayout, State.EXPANDED);
                }
                mCurrentState = State.EXPANDED;
            } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                if (mCurrentState != State.COLLAPSED) {
                    onStateChangeListener.onStateChanged(appBarLayout, State.COLLAPSED);
                }
                mCurrentState = State.COLLAPSED;
            } else {
                if (mCurrentState != State.IDLE) {
                    onStateChangeListener.onStateChanged(appBarLayout, State.IDLE);
                }
                mCurrentState = State.IDLE;
            }
        }
    }

    public interface OnStateChangeListener {
        void onStateChanged(AppBarLayout appBarLayout, State state);
    }
}
